package com.q.tqb.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.q.tqb.R;
import com.q.tqb.adapter.BaseFragmentPagerAdapter;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.fragment.PromotionFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class PromtionActivity extends BaseActivity {


    @BindView(R.id.tab_promotion)
    TabLayout mTabPromotion;

    @BindView(R.id.vp_promotion)
    ViewPager mVpPromotion;

    private BaseFragmentPagerAdapter mFragmentPagerAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_promtion;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("春节活动");

        //mPromtionAdapter = new PromtionAdapter();
        List<Fragment> fragments = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            PromotionFragment mineSendRedFragment = PromotionFragment.newInstance(i);
            fragments.add(mineSendRedFragment);
        }

        String[] tabStrings = new String[]{"02-12", "02-13", "02-14", "02-15", "02-16",
                "02-17", "02-18", "02-19", "02-20", "02-21", "02-22", "02-23"};


        mFragmentPagerAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), this, fragments, tabStrings);
        mTabPromotion.setTabMode(TabLayout.MODE_SCROLLABLE);

        mVpPromotion.setAdapter(mFragmentPagerAdapter);
        mTabPromotion.setupWithViewPager(mVpPromotion);

        mTabPromotion.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabPromotion, 10, 10);
            }
        });

        findViewById(R.id.iv_back1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.tv_right1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PromtionActivity.this, PromotionRuleActivity.class);
                startActivity(intent);
            }
        });
    }

    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        assert llTab != null;
        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }

}
